local sk__tiandao = fk.CreateSkill {

  name = "sk__tiandao",

  tags = { Skill.Compulsory, },

}



sk__tiandao:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__tiandao.name) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@sk__tiandao")
    if #mark == 0 then
      mark = { 1, 1, 1, 1 }
    end
    if mark[1] > 0 then
      player:drawCards(mark[1], sk__tiandao.name)
    end
    if player.dead then return end
    local n = mark[2]
    if n > 0 then
      local skills = {}
      for _, general in ipairs(room.general_pile) do
        local g = Fk.generals[general]
        if g.kingdom == "qun" then
          table.insertTableIfNeed(skills, g:getSkillNameList())
        end
      end
      skills = table.filter(skills, function(s)
        return not player:hasSkill(s, true)
      end)
      if #skills == 0 then return end
      local skill = table.random(skills, n)
      room:handleAddLoseSkills(player, skill, nil, true, false)
    end
    if player.dead then return end
    local n1, n2 = mark[3], mark[4]
    if n1 > 0 or n2 > 0 then
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#sk__tiandao-choose:::" .. n1 .. ":" .. n2, sk__tiandao.name, true)
      if #to > 0 then
        to = room:getPlayerById(to[1])
        if n1 > 0 and not to:isNude() then
          local cards = table.random(to:getCardIds("he"), n1)
          room:throwCard(cards, sk__tiandao.name, to, to)
        end
        if n2 > 0 and not to.dead then
          room:damage({
            from = player,
            to = to,
            damage = n2,
            damageType = fk.ThunderDamage,
            skillName = sk__tiandao.name,
          })
        end
      end
    end
  end,
})


sk__tiandao:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  room:setPlayerMark(player, "@sk__tiandao", { 1, 1, 1, 1 })
end)

sk__tiandao:addLoseEffect(function(self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@sk__tiandao", 0)
end)



return sk__tiandao
